from django.db import models
from utils.basemodel import BaseModel
from user.models import UserModel

# Create your models here.
class Topic(BaseModel):
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True, null=True)
    views = models.IntegerField(default=0)
    class Meta:
        db_table = 'topic'
    def __str__(self):
        return self.title

class UserTopic(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE,related_name="topics")
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE,related_name="users")
    class Meta:
        db_table = 'usertopic'
#帖子权限

class Permission(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'permission'
#帖子
class Post(BaseModel):
    title = models.CharField(max_length=200)
    content = models.TextField()
    topics = models.ManyToManyField(Topic, related_name='posts')
    permission = models.ForeignKey(Permission, on_delete=models.CASCADE,related_name='posts')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, related_name='posts')
    def __str__(self):
        return self.title
    class Meta:
        db_table = 'post'
#帖子图片
class Image(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='images')
    image_url = models.URLField()

    def __str__(self):
        return self.image_url
    class Meta:
        db_table = 'postimage'











